/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import ManejoExc.PersonalException;
import Negocio.Empresa;
import Negocio.Personal;
import Negocio.Rol;
import Negocio.TipoRol;
import java.util.Iterator;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Diego
 */
public class FrmRoles extends javax.swing.JDialog {

    /**
     * Creates new form FrmRoles2
     */
    DefaultTableModel modelEmpleados = new DefaultTableModel();
    DefaultListModel modelRoles = new DefaultListModel();
    Empresa unaEmpresa;
    Personal unEmpleado;
    TipoRol unTipoRol;
    Rol unRol;
    
    public FrmRoles(java.awt.Frame parent, boolean modal,Empresa unaEmpresa) {
        super(parent, modal);
        initComponents();
        this.unaEmpresa = unaEmpresa;              
        this.modelEmpleados.addColumn("Cuil");
        this.modelEmpleados.addColumn("Apellido");
        this.modelEmpleados.addColumn("Nombre");
               
        this.cargarEmpleados();   
        this.lstRoles.setModel(modelRoles);
    }
    
    /*
     * Mis Metodos
     */
    public void cargarEmpleados(){

        
        Object[]fila= new Object[3];
        //Iterator it = this.unaEmpresa.getEmpleadosActivos().entrySet().iterator();
        for (Map.Entry personal : this.unaEmpresa.getEmpleadosActivos().entrySet()) {    
           Personal unPersonal = (Personal) personal.getValue();
           fila[0] = unPersonal.getCuil();
           fila[1] = unPersonal.getApellido();
           fila[2] = unPersonal.getNombre();
           this.modelEmpleados.addRow(fila);           
        }
        this.TblEmpleados.setModel(modelEmpleados);
   }
    
    public void cargarRoles(){
        if(this.unEmpleado != null){
            this.modelRoles.clear();            
            Iterator it = this.unEmpleado.getRoles().iterator();
            while(it.hasNext()){
                   this.modelRoles.addElement((Rol) it.next());
            }             
        }
    }
    
    public void seleccionarEmpleado(){
        String cuil = this.modelEmpleados.getValueAt(0, this.TblEmpleados.getSelectedRow()).toString();
        try{
        this.unEmpleado = this.unaEmpresa.obtenerEmpleado(cuil);
        this.cargarRoles();
        }
        catch(PersonalException ex){
             JOptionPane.showMessageDialog(this, ex.getMessage());  
        }
    }
    
    public void ponerTipoRol(TipoRol unTipoRol){
        this.unTipoRol = unTipoRol;
        this.TxtTipoRol.setText(this.unTipoRol.toString());
    }
    
    public void cargarRol(){
        
        if(this.unRol != null){
            this.TxtTipoRol.setText(unRol.getUnTipoRol().toString());
            FindTipoRol.setEnabled(false);
            this.cmbDesde.setText(this.unRol.getDesde().toString());
            if(this.unRol.getHasta() != null){
                this.cmbDesde.setText(this.unRol.getHasta().toString());
            }else{
                this.cmbDesde.setText("");
            }
        }

        
    }
    
    /*
     * Fin Mis Metodos
     */

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        Empleados = new javax.swing.JScrollPane();
        TblEmpleados = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstRoles = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        TxtTipoRol = new javax.swing.JTextField();
        FindTipoRol = new javax.swing.JButton();
        cmbHasta = new datechooser.beans.DateChooserCombo();
        cmbDesde = new datechooser.beans.DateChooserCombo();
        BtnNuevo = new javax.swing.JButton();
        BtnGuardar = new javax.swing.JButton();
        BtnBorrar = new javax.swing.JButton();
        BtnAceptar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Empleados");

        TblEmpleados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        TblEmpleados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TblEmpleadosMouseClicked(evt);
            }
        });
        Empleados.setViewportView(TblEmpleados);

        jScrollPane1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jScrollPane1MouseClicked(evt);
            }
        });

        lstRoles.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        lstRoles.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstRolesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(lstRoles);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Roles");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Rol");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Desde");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("Hasta");

        FindTipoRol.setText("...");
        FindTipoRol.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FindTipoRolActionPerformed(evt);
            }
        });

        BtnNuevo.setText("Nuevo");
        BtnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnNuevoActionPerformed(evt);
            }
        });

        BtnGuardar.setText("Guardar");
        BtnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnGuardarActionPerformed(evt);
            }
        });

        BtnBorrar.setText("Borrar");
        BtnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnBorrarActionPerformed(evt);
            }
        });

        BtnAceptar.setText("Aceptar");
        BtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnAceptarActionPerformed(evt);
            }
        });

        jButton1.setText("Cancelar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cmbDesde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(TxtTipoRol, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addComponent(FindTipoRol))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(BtnNuevo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(BtnGuardar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(BtnBorrar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(BtnAceptar))))
                    .addComponent(Empleados)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(347, 347, 347)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cmbHasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Empleados, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(TxtTipoRol, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(FindTipoRol))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel4))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addComponent(cmbDesde, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(cmbHasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(BtnNuevo)
                            .addComponent(BtnGuardar)
                            .addComponent(BtnBorrar)
                            .addComponent(jButton1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                        .addComponent(BtnAceptar)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jScrollPane1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jScrollPane1MouseClicked
        // TODO add your handling code here:        
    }//GEN-LAST:event_jScrollPane1MouseClicked

    private void TblEmpleadosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TblEmpleadosMouseClicked
        // TODO add your handling code here:
        this.seleccionarEmpleado();
    }//GEN-LAST:event_TblEmpleadosMouseClicked

    private void FindTipoRolActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FindTipoRolActionPerformed
        // TODO add your handling code here:
        FrmFindTipoRol ventana = new FrmFindTipoRol(this, true,this.unaEmpresa);       
        ventana.setVisible(true);
    }//GEN-LAST:event_FindTipoRolActionPerformed

    private void BtnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnNuevoActionPerformed
        // TODO add your handling code here:
        this.unTipoRol = null;
        this.unRol = null;
        this.TxtTipoRol.setText("");
        
    }//GEN-LAST:event_BtnNuevoActionPerformed

    private void BtnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnGuardarActionPerformed
        // TODO add your handling code here:
        try{
        if(this.unRol == null){            
            this.unEmpleado.nuevoRol(this.unTipoRol,Empresa.miUtilidadDeFechas.fechaDb(this.cmbDesde.getText()));
        }else{
            //ACTUALIZO solo las fechas si cambio algo mas hay que borrar
            this.unRol.actualizar(Empresa.miUtilidadDeFechas.fechaDb(this.cmbDesde.getText()),Empresa.miUtilidadDeFechas.fechaDb(this.cmbHasta.getText()));
        }
        
        this.cargarRoles();
        }
        catch(Exception ex){
             JOptionPane.showMessageDialog(this, ex.getMessage()); 
        } 
    }//GEN-LAST:event_BtnGuardarActionPerformed

    private void BtnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnBorrarActionPerformed
        // TODO add your handling code here:
        if(this.unRol != null){
            this.unEmpleado.borrarRol(this.unRol);
            this.cargarRoles();
        }
        
    }//GEN-LAST:event_BtnBorrarActionPerformed

    private void lstRolesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstRolesMouseClicked
        // TODO add your handling code here:
        
        this.unRol = (Rol) this.modelRoles.getElementAt(this.lstRoles.getSelectedIndex());
        this.cargarRol();
    }//GEN-LAST:event_lstRolesMouseClicked

    private void BtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnAceptarActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_BtnAceptarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmRoles.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmRoles.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmRoles.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmRoles.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog 
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                FrmRoles dialog = new FrmRoles(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnAceptar;
    private javax.swing.JButton BtnBorrar;
    private javax.swing.JButton BtnGuardar;
    private javax.swing.JButton BtnNuevo;
    private javax.swing.JScrollPane Empleados;
    private javax.swing.JButton FindTipoRol;
    private javax.swing.JTable TblEmpleados;
    private javax.swing.JTextField TxtTipoRol;
    private datechooser.beans.DateChooserCombo cmbDesde;
    private datechooser.beans.DateChooserCombo cmbHasta;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lstRoles;
    // End of variables declaration//GEN-END:variables
}
